<?php

namespace Platform\Libs;

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id: Page.class.php 2806 2012-03-08 03:21:38Z liu21st $
// acttion 调用： $p->setConfig('theme', ' %first%  %upPage%   %downPage%   %end% %nowPage%/%totalPage% 页  ');

class Page {

    // 分页栏每页显示的页数
    public $rollPage = 10;
    // 页数跳转时要带的参数
    public $parameter;
    // 默认列表每页显示行数
    public $listRows = 20;
    // 起始行数
    public $firstRow;
    // 分页总页面数
    protected $totalPages;
    // 总行数
    protected $totalRows;
    // 当前页数
    protected $nowPage;
    // 分页的栏的总页数
    protected $coolPages;
    // 分页显示定制
    protected $config = array('header' => '条记录', 'prev' => '上一页', 'next' => '下一页', 'first' => '第一页', 'last' => '最后一页', 'theme' => ' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');
    // 默认分页变量名
    protected $varPage;
    protected $start_index;
    protected $end_index;
    protected $num_edge = 2;

    /**
      +----------------------------------------------------------
     * 架构函数
      +----------------------------------------------------------
     * @access public
      +----------------------------------------------------------
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
      +----------------------------------------------------------
     */
    public function __construct($totalRows, $listRows = '', $parameter = '') {
        $this->totalRows = $totalRows;
        $this->parameter = $parameter;
        $this->varPage = 'p';
        if (!empty($listRows)) {
            $this->listRows = intval($listRows);
        }
        $this->totalPages = ceil($this->totalRows / $this->listRows);     //总页数
        $this->coolPages = ceil($this->totalPages / $this->rollPage);
        $this->nowPage = !empty($_GET[$this->varPage]) ? intval($_GET[$this->varPage]) : 1;
        if (!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows * ($this->nowPage - 1);

        $ne_half = ceil($this->rollPage / 2);
        $np = $this->totalPages;
        $upper_limit = $np - $this->rollPage;
        $start = $this->nowPage > $ne_half ? max(min($this->nowPage - $ne_half, $upper_limit), 0) : 0;
        $end = $this->nowPage > $ne_half ? min($this->nowPage + $ne_half, $np) : min($this->rollPage, $np);

        $this->start_index = $start;
        $this->end_index = $end;
    }

    public function setConfig($name, $value) {
        if (isset($this->config[$name])) {
            $this->config[$name] = $value;
        }
    }

    /*
     *
     * function getInterval()  {
      var ne_half = Math.ceil(opts.num_display_entries/2);
      var np = numPages();
      var upper_limit = np-opts.num_display_entries;
      var start = current_page>ne_half?Math.max(Math.min(current_page-ne_half, upper_limit), 0):0;
      var end = current_page>ne_half?Math.min(current_page+ne_half, np):Math.min(opts.num_display_entries, np);
      return [start,end];
      }
     * */

    /**
      +----------------------------------------------------------
     * 分页显示输出
      +----------------------------------------------------------
     * @access public
      +----------------------------------------------------------
     */
    public function show($list, $key) {
        if (0 == $this->totalRows)
            return ' <a href="javascript:;" class="prev"></a>
        <p class="numBox"><i class="currPage">1</i> / <i class="totalPage">1</i></p>
        <a href="javascript:;" class="next"></a>';
        $p = $this->varPage;
        $nowCoolPage = ceil($this->nowPage / $this->rollPage);
//        if (!(substr($_SERVER['REQUEST_URI'], -1) == "?" || substr($_SERVER['REQUEST_URI'], -1) == "/" || substr($_SERVER['REQUEST_URI'], -1) == "&")) {
//            $_SERVER['REQUEST_URI'] .= "&";
//        }
        $url = $_SERVER['REDIRECT_URL'] . (strpos($_SERVER['REDIRECT_URL'], '?') ? '' : "?") . $this->parameter;
        $parse = parse_url($url);
        if (isset($parse['query'])) {
            parse_str($parse['query'], $params);
            unset($params[$p]);
            $url = $parse['path'] . '?' . http_build_query($params);
        }
        $url = urldecode($url);
        //上下翻页字符串
        $upRow = $this->nowPage - 1;
        $downRow = $this->nowPage + 1;

        $upRow = $upRow > 0 ? $upRow : 1;
        $downRow = ($downRow < $this->totalPages) ? $downRow : $this->totalPages;





//        <a href="javascript:;" class="next"></a>
        // << < > >>
//        $upPage = "&nbsp;<a class='prev' href='" . $url . "&" . $p . "=$upRow'>" . $this->config['prev'] . "</a>";
//        $downPage = "&nbsp;<a class='next' href='" . $url . "&" . $p . "=$downRow'>" . $this->config['next'] . "</a>";
        if ($this->nowPage == 1) {
            $upPage = '  <a href="javascript:;" class="prev"></a>';
        } else {
            if ($list) {
                $upPage = '<a title="前一页" class="prev"   href="' . $url . "&" . $p . "=" . ($this->nowPage - 1) . "&prid=" . $list[0][$key] . '"></a>';
            } else {
                $upPage = '<a title="前一页" class="prev"   href="' . $url . "&" . $p . "=" . ($this->nowPage - 1) . '"></a>';
            }
        }
        if ($this->nowPage == $this->totalPages) {
            $downPage = '<a href="javascript:;" class="next"></a>';
        } else {
            if ($list) {
                $downPage = '<a title="下一页" class="next"   href="' . $url . "&" . $p . "=" . ($this->nowPage + 1) . "&neid=" . $list[count($list) - 1][$key] . ' "></a>';
            } else {
                $downPage = '<a title="下一页" class="next"   href="' . $url . "&" . $p . "=" . ($this->nowPage + 1) . '"></a>';
            }
        }
//        if ($this->nowPage == $this->totalPages && $this->nowPage == 1) {
//            $upPage = "";
//            $downPage = '';
//        }


//        <p class="numBox"><i class="currPage">2</i> / <i class="totalPage">5</i></p>
//        <a href="javascript:; " class="next"></a>
//        /**
//         * 徐众写
//         */
//        $xz_start = 0;
//        $linkPage = "";
//        //如果总页数小于每页要展示的数据
//        if ($this->totalPages < $this->rollPage) {
//            for ($i = $xz_start; $i < $this->totalPages; $i++) {
//                $page = $i + 1;
//                if ($page == $this->nowPage) {
//                    $linkPage .= '<li class="active"><a href="#">' . $page . '</a></li>';
//                } else {
//                    $linkPage .= "<li><a href='" . $url . "&" . $p . "=$page'>" . $page . "</a></li>";
//                }
//            }
//        } else {
//            $xz_end = $this->nowPage + 5;
//            //如果开始页小于等于5的时候
//            if ($this->nowPage <= 5) {
//                for ($i = $xz_start; $i < $this->rollPage; $i++) {
//                    $page = $i + 1;
//                    if ($page == $this->nowPage) {
//                        $linkPage .= '<li class="active"><a href="#">' . $page . '</a></li>';
//                    } else {
//                        $linkPage .= "<li><a href='" . $url . "&" . $p . "=$page'>" . $page . "</a></li>";
//                    }
//                }
//                //如果开始页大于6
//            } else {
//                if ($xz_end > $this->totalPages) {
//                    $xz_start = $this->nowPage - 5 - ($xz_end - $this->totalPages);
//                    $xz_end = $this->totalPages;
//                } else {
//                    $xz_start = $this->nowPage - 5;
//                }
//                for ($i = $xz_start; $i < $xz_end; $i++) {
//                    $page = $i + 1;
//                    if ($page == $this->nowPage) {
//                        $linkPage .= '<li class="active"><a href="#">' . $page . '</a></li>';
//                    } else {
//                        $linkPage .= "<li><a href='" . $url . "&" . $p . "=$page'>" . $page . "</a></li>";
//                    }
//                }
//            }
//        }


        /**
         * 中间带...的 《 < 12...5678...1213 > 》这样的格式
         */
//        $linkPage = '';
//        if ($this->start_index > 0 && $this->num_edge > 0) {
//            $end2 = min($this->num_edge, $this->start_index);
//            for ($i = 0; $i < $end2; $i++) {
//                $page = $i + 1;
//                $linkPage .= "<li><a href='" . $url . "&" . $p . "=$page'>" . $page . "</a></li>";
//            }
//            if ($this->num_edge < $this->start_index) {
//                $linkPage .= '<li class=""><a href="#">' . "..." . '</a></li>';
//            }
//        }
//        for ($i = $this->start_index; $i < $this->end_index; $i++) {
//            $page = $i + 1;
//            if ($page != $this->nowPage) {
//                if ($page <= $this->totalPages) {
//                    $linkPage .= "<li><a href='" . $url . "&" . $p . "=$page'>" . $page . "</a></li>";
//                } else {
//                    break;
//                }
//            } else {
//                if ($this->totalPages != 1) {
//                    $linkPage .= '<li class="active"><a href="#">' . $page . '</a></li>';
//                }
//            }
//        }
//        if ($this->end_index < $this->totalPages && $this->num_edge > 0) {
//            if ($this->totalPages - $this->num_edge > $this->end_index) {
//                $linkPage .= '<li class=""><a href="#">' . "..." . '</a></li>';
//            }
//            $begin = max($this->totalPages - $this->num_edge, $this->end_index);
//            for ($i = $begin; $i < $this->totalPages; $i++) {
//                $page = $i + 1;
//                $linkPage .= "<li><a href='" . $url . "&" . $p . "=$page'>" . $page . "</a></li>";
//            }
//        }
        $pageStr = $upPage . '  <p class="numBox"><i class="currPage">' . $this->nowPage . '</i> / <i class="totalPage">' . $this->totalPages . '</i></p>' . $downPage;
//        $pageStr = str_replace(
//                array('%header%', '%nowPage%', '%totalRow%', '%totalPage%', '%upPage%', '%downPage%', '%first%', '%prePage%', '%linkPage%', '%nextPage%', '%end%'), array($this->config['header'], $this->nowPage, $this->totalRows, $this->totalPages, $upPage, $downPage, $theFirst, $prePage, $linkPage, $nextPage, $theEnd), $this->config['theme']);
        //$pageStr = $pageStr . "<li>" . $this->nowPage . " / " . $this->totalPages . "</li>";
        //$pageStr = $pageStr . "<div class='page-nav' id='page_nav'>";
        //$pageStr = $pageStr."到 <input id='txt_page' onkeydown=\"javascript: function pressEnter(e, p){ e = e? e:window.event; var code=e.which ? e.which : e.keyCode; if( code =='13') location.href = '".$url."&".$p."=' + p;} pressEnter(event, this.value); \"/>页";
        //$pageStr = $pageStr."<button id='btn_page'  value='确定' onclick=\"javascript:var p=document.getElementById('txt_page').value; location.href = '".$url."&".$p."=' + p;\"/>确定</button>";
        //$pageStr = $pageStr. "</div>";
        return $pageStr;
    }

}
